package thread;

public class ProducerAndCustomer {
    public static void main(String[] args) {
        Object o=new Object();
        Thread producer=new Thread(new Producer(o));
        Thread customer1=new Thread(new Customer(o,"顾客1"));
        Thread customer2=new Thread(new Customer(o,"顾客2"));
        producer.start();
        customer1.start();
        customer2.start();
    }
}
class Producer implements Runnable{
    Object o=new Object();
    public Producer(Object o){
        this.o=o;
    }
    @Override
    public void run() {
        while(true){
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (o){
                System.out.println("老板做好了包子，告诉顾客，可以开吃了");
                o.notifyAll();
            }
        }

    }
}
class Customer implements Runnable{
    String name;
    Object o=new Object();
    public Customer(Object o,String name) {
        this.o=o;
        this.name=name;
    }

    @Override
    public void run() {
        while(true){
            synchronized (o){
                System.out.println(name+"告知老板要的包子种类和数量");
                try {
                    o.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(name+"开吃");
                System.out.println("---------------");
            }
        }
    }
}
